package itbour.onetouchshow.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaMetadataRetriever;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.util.HashMap;

import itbour.onetouchshow.R;
import itbour.onetouchshow.utils.glide.GlideCircleTransform;
import itbour.onetouchshow.utils.glide.GlideRoundTransform;

/**
 * @author ymz
 */
public class ImageLoader {


    private static final ImageLoader ourInstance = new ImageLoader();
    //private static final String SHRINKURL = "?x-oss-process=image/resize,h_";
    private static final String SHRINKURL = "?x-oss-process=image/resize,h_";
    //http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,m_lfit,h_100,w_100

    public static ImageLoader getInstance() {
        return ourInstance;
    }


    public static String getShrinkImageUrl(String orangleUrl, int w, int h) {
//        return orangleUrl+SHRINKURL+(int)(h/2);
        // L_.e("显示图片" + orangleUrl + "?x-oss-process=image/resize,m_lfit,h_" + h + ",w_" + w);
        return orangleUrl + "?x-oss-process=image/resize,m_lfit,h_" + h + ",w_" + w + "/quality,Q_50";
    }


    public static String getVideoShareImageUrl(String orangleUrl) {
        return orangleUrl + "?x-oss-process=image/resize,m_lfit,h_" + 100 + ",w_" + 100 + "/quality,Q_50/watermark,image_TWluaVByb2dyYW0vc2hhcmVfcGxheS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF81MA==,g_center";
        //  http://itbour-user.oss-cn-hangzhou.aliyuncs.com/images/U670651/2017/12/15/185409188_UrSIPWN2fD/01.JPG?x-oss-process=image/resize,m_fill,h_100,w_100/quality,Q_50/watermark,image_TWluaVByb2dyYW0vc2hhcmVfcGxheS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF81MA==,g_center
    }

    public static String getShrinkImageUrl(String orangleUrl, int h) {
        return orangleUrl + SHRINKURL + h;
    }

    public static String showVideoImageShare(String orangleUrl, int w, int h) {
//        return orangleUrl+SHRINKURL+(int)(h/2);
        // L_.e("显示图片" + orangleUrl + "?x-oss-process=image/resize,m_lfit,h_" + h + ",w_" + w);
        return orangleUrl + "?x-oss-process=image/resize,m_lfit,h_" + h + ",w_" + w + "/quality,Q_50"
                + "/watermark,image_TWluaVByb2dyYW0vc2hhcmVfcGxheS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF81MA==,g_center";
    }

    public void showRealSizeImage(String url, ImageView imageView) {
        Glide.with(UIUtils.getContext()).setDefaultRequestOptions(new RequestOptions()
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(false).
                        diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(new ColorDrawable(UIUtils.getColor(R.color.apptheme))))
                .load(url).into(imageView);
    }

    public void showImage(Context context, String url, ImageView imageView) {
//        Glide.with(UIUtils.getContext()).setDefaultRequestOptions(new RequestOptions()
//        .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
//        .centerCrop()).load(url).into(imageView);
        // Glide.with(context).load(shrinkImageUrl).thumbnail(0.1f).into(imageView);
//        Glide.with(context).applyDefaultRequestOptions(new RequestOptions().apply(new RequestOptions().skipMemoryCache(true)).placeholder(R.drawable.place_icon).fitCenter()).load(url).into(imageView);
        Glide.with(context).applyDefaultRequestOptions(new RequestOptions().apply(new RequestOptions().skipMemoryCache(true)).placeholder(R.drawable.place_icon).fitCenter()).load(url).into(imageView);
//        Glide.with(UIUtils.getContext())
//                .load(url)
//                .apply(new RequestOptions()
//                        .placeholder(R.drawable.empty_drawable))
//                .into(imageView);
    }

    public void showUserHead(Context context, String url, ImageView imageView) {
        Glide.with(context).applyDefaultRequestOptions(new RequestOptions().apply(new RequestOptions().skipMemoryCache(true)).placeholder(R.drawable.place_icon).centerCrop()).load(url).into(imageView);
    }

    public void showCircleImage(Context activity, String url, ImageView imageView) {
//        Glide.with(UIUtils.getContext()).setDefaultRequestOptions(new RequestOptions()
//        .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
//        .centerCrop()).load(url).into(imageView);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        // Glide.with(activity).applyDefaultRequestOptions(new RequestOptions()).load(url).into(imageView);
        Glide.with(activity).applyDefaultRequestOptions(new RequestOptions().skipMemoryCache(true).placeholder(R.drawable.place_icon).error(R.drawable.place_icon).centerCrop().transform(new GlideCircleTransform()).dontAnimate()).load(url).into(imageView);
    }


    public void showCircleImageWithBorder(Context activity, String url, ImageView imageView) {
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//        MultiTransformation multi = new MultiTransformation(
//                new BlurTransformation(25),
//                new RoundedCornersTransformation(128, 0, RoundedCornersTransformation.CornerType.BOTTOM));
        Glide.with(activity).applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.place_icon)
                .error(R.drawable.place_icon).skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop().transform(new GlideCircleTransform()).dontAnimate()
        ).load(url).into(imageView);

        //   showCircleImageWithBorderCenterCrop(activity,url,imageView);

    }


    public void showCircleImageWithBorderCenterCrop(Context activity, String url, ImageView imageView) {
//        Glide.with(UIUtils.getContext()).setDefaultRequestOptions(new RequestOptions()
//        .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
//        .centerCrop()).load(url).into(imageView);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(activity).applyDefaultRequestOptions(new RequestOptions().skipMemoryCache(true).placeholder(R.drawable.place_icon).error(R.drawable.place_icon).centerCrop().transform(new GlideRoundTransform(activity)).dontAnimate()).load(url).into(imageView);
//         Glide.with(activity).applyDefaultRequestOptions(new RequestOptions()).load(url).into(imageView);
        //Glide.with(activity).applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.place_icon).error(R.drawable.place_icon).transform(new CenterCrop()).transform(new GlideCircleTransform(10)).dontAnimate()).load(url).into(imageView);
    }

    public void showyouhuijuanCircleImage(Context activity, String url, ImageView imageView) {
//        Glide.with(UIUtils.getContext()).setDefaultRequestOptions(new RequestOptions()
//        .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
//        .centerCrop()).load(url).into(imageView);
        //Glide.with(activity).applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.place_icon2).fitCenter().error(R.drawable.place_icon2).transform(new GlideCircleTransform())).load("").into(imageView);
    }

    public void showImageNoplace(Activity activity, String url, ImageView imageView) {
//        Glide.with(UIUtils.getContext()).setDefaultRequestOptions(new RequestOptions()
//        .skipMemoryCache(false).diskCacheStrategy(DiskCacheStrategy.ALL)
//        .centerCrop()).load(url).into(imageView);
        Glide.with(activity).applyDefaultRequestOptions(new RequestOptions()).load(url).into(imageView);
//        Glide.with(UIUtils.getContext())
//                .load(url)
//                .apply(new RequestOptions()
//                        .placeholder(R.drawable.empty_drawable))
//                .into(imageView);
    }

    public void load(int resId, ImageView iv) {
        Glide.with(UIUtils.getContext()).setDefaultRequestOptions(new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.ic_launcher)).load(resId).into(iv);
    }

    /**
     * 加载第三秒的帧数作为封面
     */
    private void loadCover(ImageView imageView, String url) {
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.mipmap.ic_launcher);
        Glide.with(UIUtils.getContext())
                .setDefaultRequestOptions(
                        new RequestOptions()
                                .frame(3000000)
                                .centerCrop()
                                .error(R.mipmap.ic_launcher)
                                .placeholder(R.mipmap.ic_launcher))
                .load(url)
                .into(imageView);
    }


    // 获取视频缩略图
    public Bitmap getVideoThumbnail(String filePath) {
        Bitmap b = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            if (filePath.startsWith("http") || filePath.startsWith("https")) {
                retriever.setDataSource(filePath, new HashMap());
            } else {
                retriever.setDataSource(filePath);
            }
            retriever.setDataSource(filePath);
            b = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return b;
    }

    /**
     * 加载第三秒的帧数作为封面
     */
    public void showRundImage(String url, ImageView imageView) {
        Glide.with(UIUtils.getContext()).applyDefaultRequestOptions(new RequestOptions()
                .transform(new GlideCircleTransform())).load(url).into(imageView);
    }

}
